<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 价格管理控制器
 */
class Price extends AdminBase
{

    /**
     * 价格列表
     */
    public function list()
    {
        $where = $this->logicPrice->getWhere($this->param);
        
        $this->assign('list', $this->logicPrice->getPriceList($where));
        
        return $this->fetch('price_list');
    }
    
    /**
     * 价格趋势
     */
    public function trend()
    {
        // 如果是AJAX请求，返回JSON数据
        if (request()->isAjax()) {
            $result = $this->logicPrice->getPriceTrend($this->param);
            return json($result);
        }
        
        // 否则返回页面
        // $where = $this->logicPrice->getWhere($this->param);
        
        // 传递原始参数而不是where条件，因为getPriceTrend需要product_id等参数
        $this->assign('trend_data', $this->logicPrice->getPriceTrend($this->param));
        
        return $this->fetch('price_trend');
    }
    

    
    /**
     * 价格添加
     */
    public function add()
    {
        IS_POST && $this->jump($this->logicPrice->priceAdd($this->param));
        
        // 为添加操作设置空的info数组，确保模板中的默认值正确显示
        $this->assign('info', []);
        
        return $this->fetch('price_edit');
    }
    
    /**
     * 价格编辑
     */
    public function edit()
    {
        // 禁用布局输出，用于弹窗显示
        $this->view->engine->layout(false);
        
        IS_POST && $this->jump($this->logicPrice->priceEdit($this->param));
        
        $info = $this->logicPrice->getPriceInfo(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        
        return $this->fetch('price_edit');
    }
    
    /**
     * 价格删除
     */
    public function delete()
    {
        $this->jump($this->logicPrice->priceDel($this->param));
    }
    
    /**
     * 价格统计
     */
    public function stats()
    {
        $this->assign('stats', $this->logicPrice->getPriceStats($this->param));
        
        return $this->fetch('price_stats');
    }
    
    /**
     * 价格导出
     */
    public function export()
    {
        $where = $this->logicPrice->getWhere($this->param);
        
        $this->logicPrice->exportPriceList($where);
    }
    
    /**
     * 价格监控设置
     */
    public function monitor()
    {
        IS_POST && $this->jump($this->logicPrice->priceMonitor($this->param));
        
        $this->assign('monitor_list', $this->logicPrice->getMonitorList());
        
        return $this->fetch('price_monitor');
    }
    
    /**
     * 价格详情
     */
    public function detail()
    {
        // 禁用布局输出，用于弹窗显示
        $this->view->engine->layout(false);
        
        // 如果是AJAX请求，返回JSON数据
        if (request()->isAjax()) {
            $result = $this->logicPrice->getPriceDetail($this->param);
            return json($result);
        }
        
        // 否则返回弹窗页面
        $info = $this->logicPrice->getPriceDetail($this->param);
        $this->assign('info', $info);
        
        return $this->fetch('price_detail');
    }
    
    /**
     * 价格历史
     */
    public function history()
    {
        $result = $this->logicPrice->getPriceHistory($this->param);
        return json($result);
    }

    /**
     * 价格变动记录（按时间倒序，用于后台管理查看）
     * 作者: 王勇程 (laughskydragon@qq.com)
     */
    public function changeRecords()
    {
        $result = $this->logicPrice->getPriceChangeRecords($this->param);
        return json($result);
    }
    
    /**
     * 价格预警
     */
    public function alert()
    {
        $this->jump($this->logicPrice->savePriceAlert($this->param));
    }
    
    /**
     * 价格预警设置（弹窗）
     */
    public function saveAlert()
    {
        // 禁用布局输出，用于弹窗显示
        $this->view->engine->layout(false);
        
        if (IS_POST) {
            $result = $this->logicPrice->savePriceAlert($this->param);
            return json($result);
        }
        
        // 获取预警设置信息
        $this->assign('alert_info', $this->logicPrice->getAlertInfo($this->param));
        
        return $this->fetch('price_alert');
    }
    
    /**
     * 批量调价
     */
    public function batchAdjust()
    {
        // 禁用布局输出，用于弹窗显示
        $this->view->engine->layout(false);
        
        if (IS_POST) {
            $result = $this->logicPrice->batchPriceAdjust($this->param);
            return json($result);
        }
        
        // 获取批量调价页面
        $this->assign('batch_info', $this->logicPrice->getBatchInfo($this->param));
        
        return $this->fetch('price_batch_adjust');
    }
    
    /**
     * 获取价格信息
     */
    public function getPriceInfo()
    {
        $result = $this->logicPrice->getPriceInfo($this->param);
        return json($result);
    }
    
    /**
     * 价格变动
     */
    public function change()
    {
        $result = $this->logicPrice->savePriceChange($this->param);
        return json($result);
    }
    
    /**
     * 导出价格历史
     */
    public function exportHistory()
    {
        $where = $this->logicPrice->getWhere($this->param);
        
        $this->logicPrice->exportPriceHistory($where);
    }
}